package com.qqbot.service.group;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qqbot.enums.GroupUserStatusEnum;
import com.qqbot.exception.BusinessException;
import com.qqbot.model.group.UserGroupRelation;
import com.qqbot.dao.group.UserGroupRelationDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qqbot.utils.CollectionUtil;
import com.qqbot.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fyq
 * @since 2023-03-02
 */
@Service
public class UserGroupRelationServiceImpl extends ServiceImpl<UserGroupRelationDao, UserGroupRelation> implements UserGroupRelationService {

    @Override
    public boolean isItBlacklist(String bot, String qq, String groupNo) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, qq, groupNo)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        QueryWrapper<UserGroupRelation> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("qq", qq);
        query.eq("group_no", groupNo);
        query.eq("status", GroupUserStatusEnum.BLACKLIST.getCode());
        return this.count(query) > 0;
    }

    @Override
    public boolean saveOrUpdateGroupUser(String bot, String qq, String groupNo, Integer status, Integer integral) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, qq, groupNo) || Objects.isNull(status)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        UserGroupRelation userGroupRelation = null;
        QueryWrapper<UserGroupRelation> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("qq", qq);
        query.eq("group_no", groupNo);
        userGroupRelation = this.getOne(query);
        if (Objects.isNull(userGroupRelation)) {
            userGroupRelation = new UserGroupRelation();
        }
        userGroupRelation.setBot(bot);
        userGroupRelation.setQq(qq);
        userGroupRelation.setGroupNo(groupNo);
        userGroupRelation.setStatus(status);
        if (Objects.nonNull(integral)) {
            userGroupRelation.setIntegral(integral);
            userGroupRelation.setSignDate(new Date());
        }
        return this.saveOrUpdate(userGroupRelation);
    }

    @Override
    public List<UserGroupRelation> listByGroupAndQqList(String bot, String groupNo, List<String> qqList) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, groupNo) || CollectionUtil.isEmpty(qqList)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        QueryWrapper<UserGroupRelation> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("group_no", groupNo);
        query.in("qq", qqList);
        query.ne("status", GroupUserStatusEnum.BLACKLIST.getCode());
        return this.list(query);
    }

    @Override
    public boolean updateStatusByGroupAndQqList(String groupNo, List<String> qqList) throws BusinessException {
        if (StringUtils.isBlank(groupNo) || CollectionUtil.isEmpty(qqList)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        UpdateWrapper<UserGroupRelation> update = new UpdateWrapper<>();
        update.set("status", GroupUserStatusEnum.NORMAL.getCode());
        update.eq("group_no", groupNo);
        update.in("qq", qqList);
        return this.update(update);
    }

    @Override
    public UserGroupRelation getByQqAndGroupNo(String bot, String qq, String groupNo) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, qq, groupNo)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        QueryWrapper<UserGroupRelation> query = new QueryWrapper<>();
        query.eq("bot", bot);
        query.eq("qq", qq);
        query.eq("group_no", groupNo);
        return this.getOne(query);
    }

    @Override
    public boolean addIntegral(String bot, String qq, String groupNo, Integer integral) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, qq, groupNo) || Objects.isNull(integral)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        return this.baseMapper.addIntegral(bot, qq, groupNo, integral);
    }

    @Override
    public List<UserGroupRelation> integralRanking(String bot, String groupNo) throws BusinessException {
        if (StringUtils.isAnyBlank(bot, groupNo)) {
            throw new BusinessException(Constant.PARAMS_IS_NULL_MESSAGE);
        }
        return this.baseMapper.integralRanking(bot, groupNo);
    }
}
